/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.usv.library.servlet;

import com.usv.library.entity.Author;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author nika
 */
//@WebServlet(name = "ViewAuthors", urlPatterns = {"/ViewAuthors"})
public class ViewAuthors extends HttpServlet {

   
    List<Author> result;
    

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8"); 
        PrintWriter out = response.getWriter();  
//        String name = request.getParameter("name");  
//        String password = request.getParameter("password");  
        Session session = null;  
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();  
        session = sessionFactory.openSession();  
        Transaction transaction = session.beginTransaction();  
        
//         HQL queries should specify the entity class, not the table name.
        result = session.createQuery("from Author order by lastName").list();
          
//        User user = new User();  
//        user.setName(name);  
//        user.setPassword(password);  
//        session.save(user);  
        transaction.commit();  
        session.flush();  
          
        session.close();  
        out.println(result);  
        
        request.setAttribute("Authors", result);
//
////        session.setAttribute("listAuthors", result);
////        request.getRequestDispatcher("/index.jsp").forward(request, response);
          request.getRequestDispatcher("/authors.jsp").forward(request, response);
//          session.close(); 
               out.flush();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}